Skip to main content

Go

This doc shows how to use gotestsum to generate the JUnit XML reports. gotestsum runs tests using go test --json and produces formatted test output, including support for the JUnit XML format. This is a structured summary of the test run that contains test failures, successes elapsed time, as reported by the test runner.

  1. Update your CI workflow to install gotestyourself/gotestsum, create a directory for the JUnit XML files, and then use gotestsum to run the test suite.

This example Golang project uses Github Actions for CI, so we're updating our CI script in the .github/workflows/ directory, and we're using GitHub Actions syntax.

First, we add a step to the CI workflow to install gotestsum. Then, before running the test suite (integration or unit tests), we create a directory for the JUnit XML files. And finally, we replace go test with the corresponding gotestsum command. The diff of these changes looks like this:

  jobs:
with:
go-version: '1.16'
+ - name: Set up gotestsum
+ run: |
+ go install gotest.tools/gotestsum@latest // or go get module dependency
+
- name: Run tests
run: |
- go test ./...
+ mkdir -p tmp/test-results
+ gotestsum --junitfile tmp/test-results/gotestsum-report.xml ./...

If you're using a different CI service, apply the corresponding changes wherever your CI script is defined (e.g., .circleci/config.yml for CircleCI, etc.).

  1. Commit this change to your repository.
git commit -am "Update CI to generate JUnit XML for test results"

The final result of these changes should resemble commit 5e497f6 in the buildpulse-example-go repository - or view the README for setup repository instructions.

Whether your test runs on pull requests or period builds, you should now see generated junit xml files (with tests failed and passed) in your test reports directory - first step in detecting flaky tests.